package cn.xiaobai;

import cn.xiaobai.tools.Packager;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.IOException;

/**
 * @Author yangdaji
 * @Date 2023/10/26
 * @PackageName cn.xiaobai
 * @ClassName RepackageMojo
 */
@Mojo(name = "repackage", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
public class RepackageMojo extends AbstractRepackageMojo {

    @Parameter(defaultValue = "${project.build.finalName}", readonly = true)
    private String finalName;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        repackage();
    }

    private void repackage() throws MojoExecutionException {
        getLog().info("---------- maven package after repackage ----------------");
        Artifact sourceArtifact = getSourceArtifact(null);
        // 获取编译输出目录和maven 打包工件文件
        Packager packager = getConfiguredPackager(() -> new Packager(sourceArtifact.getFile()));
        try {
            packager.rePackager();
        } catch (IOException e) {
            getLog().error("repackage fail: " + e.getMessage());
        }
    }



}
